<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品基本信息维度功能
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/goods/goodsBasic.class.php');

class ajax_goodsBasic {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1:
                    $this->goodsBasicSearch();  //商品基本信息维度查询
                break;
                case 2:
                    $this->editgoodsBasic();    //获得要编辑的信息
                break;
                case 3:
                    $this->searchsort();        //获得最大的序号
                break;
                case 4:
                    $this->goodsBasicAdd();     //保存基本信息维度
                break;
                case 5:
                    $this->goodsBasicDel();     //商品基本信息的删除
                break;
                case 6:
                    $this->goodsBasicChange();  //商品维度的使用状态的修改
                break;
                default:
                    backendError();
            }

        } else {
        	backendError();
        }

    }

    /**
     * 商品基本信息维度查询
     */
    private function goodsBasicSearch() {

        global $G_PGLOB;

        $goodsBasic = new goodsBasic($G_PGLOB->DBCA);
        $goosBasicInfo = $goodsBasic->searchgoodsBasic($_REQUEST);

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );



        if ($goosBasicInfo) {

            $edit_permission = $G_PGLOB->has_permission(533);

            foreach($goosBasicInfo as &$s_l) {

                $goods_name = ' ';
                $basicVal 	= ' ';

                if ($s_l['goods_basic']) {
                	foreach($s_l['goods_basic'] as $k=>&$v) {
                		$goods_name .= $v['basicName'] . '　';
                		$basicVal 	.= $v['basicVal'] . '　';
                	}
                }
                
                if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['basicGroupID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="goodsBasic.edit(' . $s_l['basicGroupID'] . ')">维护值</a>' ,
                            $s_l['sortOrder'],
                            $s_l['french'],
                            $s_l['chinese'],
                            $goods_name,
							$basicVal,
                            $s_l['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick="goodsBasic.doAction(' . $s_l['basicGroupID'] . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="goodsBasic.doAction(' . $s_l['basicGroupID'] . ', 1 ) " >禁用</a>'
                        )

                    );
                } else {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['basicGroupID'],
                        'cell' => array(
                            $s_l['basicGroupID'],
                            $s_l['sortOrder'],
                            $s_l['french'],
                            $s_l['chinese'],
                            $goods_name,
                        	$basicVal,
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )

                    );
                }

            }
        }

        echo json_encode( $result_array );

    }

    /**
     * 待编辑信息
     */
    private  function editgoodsBasic(){

        global $G_PGLOB;

        if (!isset($_REQUEST['basicGroupID']) || !is_numeric($_REQUEST['basicGroupID'])) {
            return array( 'code' => 400 , 'message' => '基本信息组ID不存在' );
        }

        $arg_array = array('basicGroupID' => $_REQUEST['basicGroupID']);
        $goodsBasic = new goodsBasic( $G_PGLOB->DBCA ) ;
        $result_data  = $goodsBasic->searchgoodsBasic( $arg_array );
        
        if ($result_data) {
        	foreach ($result_data[0]['goods_basic'] as $k=>&$v) {
        		if (empty($v['basicVal'])) {
        			$v['basicVal'] = '&nbsp;';
        		}
        	}
        }
 
        $result = array(
            'basicGroupID'	    => $result_data[0]['basicGroupID'],
            'chinese'	        => $result_data[0]['chinese'],
            'french'	        => $result_data[0]['french'],
            'useFlg'	        => $result_data[0]['useFlg'],
            'sortOrder'	        => $result_data[0]['sortOrder'],
            'goods_basic'       => $result_data[0]['goods_basic']
        );
        
        

        echo json_encode($result);
    }

    /**
     * 最大序列号查询
     */
    private function searchsort(){

        global $G_PGLOB;

        $goodsBasic = new goodsBasic( $G_PGLOB->DBCA ) ;

        $sortOrder = $goodsBasic->searchSort();

        echo json_encode(array('sortOrder' => $sortOrder));
    }

    /**
     * 保存商品基本信息维度
     */
    private function goodsBasicAdd() {
        global $G_PGLOB;

        $goodsBasic = new goodsBasic($G_PGLOB->DBCA);
        $retArr = $goodsBasic->savegoodsBasic($_REQUEST);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '数据保存成功' , 'data' => $retArr['data'] )));
        }
    }

    /**
     * 商品基本信息维度的删除
     */
    private function goodsBasicDel() {

        global $G_PGLOB;

        //接受要删除的参数
        $basicGroupID = $_REQUEST['p_array'][0];

        $goodsBasic = new goodsBasic($G_PGLOB->DBCA);
        $retArr = $goodsBasic->delgoodsBasic($basicGroupID);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品基本信息维度删除成功' )));
        }

    }

    /**
     * 商品维度的使用状态的修改
     */
    private function goodsBasicChange() {

        global $G_PGLOB;

        $basicGroupID  = $_REQUEST['basicGroupID'];
        $useFlg        = $_REQUEST['useFlg'];

        $goodsBasic = new goodsBasic($G_PGLOB->DBCA);
        $retArr = $goodsBasic->changeuseFlg($basicGroupID,$useFlg);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品维度使用状态修改成功' )));
        }

    }
}